package Hibernate_web.dao;


import Hibernate_web.HibernateUtil;
import Hibernate_web.model.Player;
import org.hibernate.Session;

import java.sql.SQLException;
import java.util.List;

public class PlayerDao implements InterfacePlayerDao {

    @Override
    public List<Player> getAllPlayers() throws SQLException {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        List<Player> toSend = session.createCriteria(Player.class).list();
        session.getTransaction().commit();
        return toSend;
    }

    @Override
    public Player getPlayerById(Integer id) throws SQLException {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        Player player = (Player) session.load(Player.class,id);
        session.getTransaction().commit();
        return player;

    }

    @Override
    public Player addPlayer(Player player) throws SQLException {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        session.save(player);
        session.getTransaction().commit();
        return player;
    }
}



